package com.nana.petadventure.net
{
	import com.nana.framework.net.DataChannel;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class DataProxy extends DataChannel
	{
		public static const TIMEOUT:int = 150000;
		public static const HEARTBEAT_HTTP:int = 150;
		
		private static var _instance:DataProxy;
		
		private var _httpSendList:Array;
		private var _httpHeartBeatTimer:Timer;
		
		public function DataProxy(){
			if (_instance == null){
				super();
				this._httpSendList = [];
				this._httpHeartBeatTimer = new Timer(HEARTBEAT_HTTP);
				this._httpHeartBeatTimer.addEventListener(TimerEvent.TIMER, this.httpHeartBeatHandler);
				this._httpHeartBeatTimer.start();
				_instance = this;
			} else {
				throw (new Error("实例化单例类出错-DataProxy"));
			}
		}
		
		public static function getInstance():DataProxy{
			if (_instance == null){
				_instance = new DataProxy();
			}
			return _instance;
		}
		
		override public function send(cmd:String, data:Object=null, other:Object=null):void{
			this._httpSendList.push({
				cmd:cmd,
				data:data,
				other:other
			});
		}
		
		private function httpHeartBeatHandler(event:TimerEvent):void{
			if (this._httpSendList.length > 0){
				var httpObj:Object = this._httpSendList.shift();
				super.send(httpObj.cmd, httpObj.data, httpObj.other);
			}
		}
	}
}